using System;
using System.Collections.Generic;
using System.Text;
using UMC.Data;

namespace UMC.WorkFlow.Subassembly
{
    [UMC.WorkFlow.DefaultDesign(ArgumentType = UMC.WorkFlow.ArgumentType.Command, ArgumentValue = "SqlSetting")]
    public class SqlSetting:IJSONType
    {
        /// <summary>
        /// 数据库访问配置节名
        /// </summary>
        [DesignAttribute(Caption = "数据库访问配置节名")]
        public string DbProviderKey
        {
            get;
            set;
        }
        /// <summary>
        /// 数据库脚本
        /// </summary>
        [DesignAttribute(Caption = "Sql文本")]
        public string SqlText
        {
            get;
            set;
        }
          protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public virtual Func<object> GetInstance(string prototypeName)
        {
            return () => String.Empty;
            //throw new NotImplementedException();
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            { 
                case "SqlText":
                    this.SqlText = value as string;
                    break;
                case "DbProviderKey":
                    this.DbProviderKey = value as string;// Utility.Parse(value as string, CommandType.Undefine);
                    break;  
            }
        }
        protected virtual void WriteProperty(System.IO.TextWriter writer)
        {
 
            if (String.IsNullOrEmpty(this.DbProviderKey)==false)
            {
                writer.Write(',');
                JSON.Serialize("DbProviderKey", this.DbProviderKey, writer);
            }
 
            if (String.IsNullOrEmpty(this.SqlText)==false)
            {
                writer.Write(',');
                JSON.Serialize("SqlText", this.SqlText, writer);
            }
        }

        public void Write(System.IO.TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');

        }
    }
    [UMC.WorkFlow.DefaultDesign(ArgumentType = UMC.WorkFlow.ArgumentType.Command, ArgumentValue = "TextSetting")]
    public class TextSetting:IJSONType
    {
        public UMC.WorkFlow.CommandType CommandType
        {
            get;
            set;
        }
        /// <summary>
        /// 数据库脚本
        /// </summary>
        [DesignAttribute(Caption = "文本格式")]
        public string Text
        {
            get;
            set;
        }
        protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public virtual Func<object> GetInstance(string prototypeName)
        {
            return () => String.Empty;
            //throw new NotImplementedException();
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            { 
                case "Text":
                    this.Text = value as string;
                    break;
                case "CommandType":
                    this.CommandType = Utility.Parse(value as string, CommandType.Undefine);
                    break;  
            }
        }
        protected virtual void WriteProperty(System.IO.TextWriter writer)
        {
 
            if (String.IsNullOrEmpty(this.Text)==false)
            {
                writer.Write(',');
                JSON.Serialize("Text", this.Text, writer);
            }
            if (this.CommandType > CommandType.Undefine)
            {
                writer.Write(',');
                JSON.Serialize("CommandType", this.CommandType, writer);
            } 
        }

        public void Write(System.IO.TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');
        }
    }
}
